import 'package:common/common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ContentTextFieldWidget extends StatefulWidget {
  const ContentTextFieldWidget({
    super.key,
    this.onChanged,
    this.onSubmit,
    this.height,
    this.border,
    this.borderRadius,
    this.top = 20,
    this.number = false,
    this.hintText = '请输入手机号',
    this.backgroundColor = const Color(0xFFF7F8F9),
    this.maxLength = 10,
    this.text = '',
  });
  final double? height;

  final Function? onChanged;
  final Function? onSubmit;

  final Border? border;
  final double? borderRadius;
  final Color? backgroundColor;
  final String? hintText;
  final double? top;
  final bool number;
  final int? maxLength;
  final String text;

  @override
  State<ContentTextFieldWidget> createState() => _ContentTextFieldWidgetState();
}

class _ContentTextFieldWidgetState extends State<ContentTextFieldWidget> {
  TextEditingController controller = TextEditingController();
  FocusNode focusNode = FocusNode();

  @override
  void initState() {
    controller = TextEditingController(text: widget.text);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      height: widget.height ?? 50.h,
      margin: EdgeInsets.only(top: widget.top ?? 20),
      padding: EdgeInsets.only(left: 10.w, right: 10.w),
      decoration: BoxDecoration(
          color: widget.backgroundColor,
          borderRadius: BorderRadius.circular(widget.borderRadius ?? 8),
          border: widget.border ?? Border.all(color: const Color(0xffE9E9E9), width: 0)),
      child: Row(
        children: [
          Expanded(
            child: Container(
              padding: EdgeInsets.only(left: 10.h),
              alignment: Alignment.centerLeft,
              child: SizedBox(
                child: TextField(
                  maxLength: 10,
                  focusNode: focusNode,
                  controller: controller,
                  obscureText: false,
                  // cursorWidth: 2,
                  maxLines: 1,
                  cursorColor: const Color(0xFF2CC0B1),
                  // cursorHeight: 18.h,
                  autofocus: false, // 指不自动获取焦点
                  onChanged: (value) {
                    widget.onChanged?.call(value);
                    setState(() {});
                  },
                  onSubmitted: (value) {
                    widget.onSubmit?.call();
                  },
                  style: TextStyle(color: const Color(0xFF000000), fontSize: 14.sp),
                  decoration: InputDecoration(
                    hintText: widget.hintText,
                    counterText: '',
                    hintStyle: TextStyle(color: const Color(0xFF999999), fontSize: 14.sp),
                    border: InputBorder.none,
                    suffixText: widget.number ? '${controller.text.length} / 10' : '',
                  ),
                ),
              ),
            ),
          ),
          if (controller.text.isNotEmpty)
            Container(
              margin: const EdgeInsets.only(left: 10),
              child: Icon(Icons.cancel, size: 20.w, color: const Color(0xFFADB4B3)).onTap(() {
                controller.text = '';
                widget.onChanged?.call('');
                setState(() {});
              }),
            )
        ],
      ),
    );
  }
}
